using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AdventureWorks.Cinema.Client.Windows
{
   
    internal static class DataTableHelper
    {
        public static DataColumn AddColumn(DataTable target, string name, 
            string caption, Type dataType)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (name == null)
                throw new ArgumentNullException("name");

            if (caption == null)
                throw new ArgumentNullException("caption");

            if (dataType == null)
                throw new ArgumentNullException("dataType");

            DataColumn column = new DataColumn();
            column.Caption = caption;
            column.ColumnName = name;
            column.DataType = dataType;

            target.Columns.Add(column);

            return column;
        }

        public static DataColumn AddColumn(DataTable target, string name, 
            string caption, Type dataType, MappingType columnMapping)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (name == null)
                throw new ArgumentNullException("name");

            if (caption == null)
                throw new ArgumentNullException("caption");

            if (dataType == null)
                throw new ArgumentNullException("dataType");

            DataColumn column = AddColumn(target, name, caption, dataType);
            column.ColumnMapping = columnMapping;

            return column;
        }

        public static void FillGridView(DataTable dataTable, 
            DataGridView targetGrid)
        {
            foreach (DataColumn dataColum in dataTable.Columns)
            {
                if (dataColum.ColumnMapping != MappingType.Hidden)
                {
                    DataGridViewTextBoxColumn col 
                        = new DataGridViewTextBoxColumn();
                    col.DataPropertyName = dataColum.ColumnName;
                    col.HeaderText = dataColum.Caption;
                    targetGrid.Columns.Add(col);
                }
            }
        }
    }
}
